package com.top.dao;


import com.top.bean.System_user;
import com.top.bean.User_detail_info;
import com.top.util.DBconn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class userDaoImpl implements userDao{

    @Override
    public List<User_detail_info> getInfo() {
        DBconn.init();//连接数据库
        ResultSet rs = DBconn.selectSql("select * from user_detail_info");
        User_detail_info user = new User_detail_info();
        List<User_detail_info> users = new ArrayList<>();
        try {
            while (rs.next()){
                String real_name = rs.getString("real_name");
                System.out.println(real_name);
                int sex = rs.getInt("sex");
                int certify_type = rs.getInt("certify_type");
                String certify_num = rs.getString("certify_num");
                int type = rs.getInt("type");
                String province = rs.getString("province");
                String city = rs.getString("city");
                user.setReal_name(real_name);
                user.setSex(sex);
                user.setCertify_type(certify_type);
                user.setCertify_num(certify_num);
                user.setType(type);
                user.setProvince(province);
                user.setCity(city);
                users.add(user);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        DBconn.closeConn();
        return users;
    }

    @Override
    public boolean login(String user_name, String password) {
        DBconn.init();//连接数据库
        boolean flag = false;//判断是否成功
        ResultSet rs = DBconn.selectSql("select * from system_user where user_name ='" + user_name + "'");//查询是否有该用户
        try {
            while (rs.next()){
                //判断查出的用户名字和密码是否和输入的相同
                if (rs.getString("password").equals(password) && rs.getString("user_name").equals(user_name)){
                    flag = true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBconn.closeConn();//关闭连接
        return flag;
    }

    @Override
    public int insert(System_user user,User_detail_info user_info) {
        DBconn.init();//连接数据库
        DBconn.addUpdDel("insert into system_user(user_name,password) value (\""+user.getUser_name()+"\",\""+user.getPassword()+"\");");
        ResultSet rs = DBconn.selectSql("select user_id from system_user where user_name=\"" + user.getUser_name() + "\" and password=\"" + user.getPassword()+"\";");
        int user_id=0;
        try {
            rs.next();
            user_id = rs.getInt("user_id");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        int i = DBconn.addUpdDel("insert into user_detail_info(real_name,sex,province,city,certify_type,certify_num,birthday,type,remarks,user_id)value(\"" +
                user_info.getReal_name() + "\"," +
                user_info.getSex() + ",\"" +
                user_info.getProvince() + "\",\"" +
                user_info.getCity() + "\"," +
                user_info.getCertify_type() + ",\"" +
                user_info.getCertify_num() + "\"," +
                user_info.getBirthday() + "," +
                user_info.getType() + ",\"" +
                user_info.getRemarks() + "\"," +
                user_id + ");"

        );
        DBconn.closeConn();//关闭连接
        return i;
    }
}

